﻿using System;
using System.Text;

namespace Reactor.Messaging
{
    [Serializable]
    public class PubSubChannel : MessagingChannel
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PubSubChannel"/> class.
        /// </summary>
        public PubSubChannel() {}

        /// <summary>
        /// Initializes a new instance of the <see cref="PubSubChannel"/> class.
        /// </summary>
        /// <param name="topicName">Name of the topic.</param>
        public PubSubChannel(string topicName)
        {
            DestinationName = topicName;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PubSubChannel"/> class.
        /// </summary>
        /// <param name="topicName">The name.</param>
        /// <param name="isTransactional">if set to <c>true</c> [is transactional].</param>
        public PubSubChannel(string topicName, bool isTransactional)
        {
            DestinationName = topicName;
            IsTransactional = isTransactional;
        }

        #endregion

        /// <summary>
        /// Gets typed version of the MessagingChannel name. That is, it's prefixed with 'topic://' to denote the MessagingChannel is a subscription topic.
        /// </summary>
        /// <returns></returns>
        public override string GetTypedDestinationName()
        {
            var sb = new StringBuilder(2);
            sb.Append("topic://");
            sb.Append(DestinationName);

            return sb.ToString();
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="PubSubChannel"/>.
        /// </summary>
        /// <param name="stringValue">The string name.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator PubSubChannel(string stringValue)
        {
            return new PubSubChannel(stringValue);
        }
    }
}
